package com.gdth.bank.adapter;

import java.io.File;
import java.io.IOException;
import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.gdth.bank.R;
import com.gdth.bank.widget.image.Bimp;
import com.gdth.database.bean.TAppFjxxb;
import com.gdth.model.Item;
import com.gdth.utils.ToolUtil;

public class CjkhFjWscListViewAdapter extends BaseAdapter {  
	
	private int layout_id;
    private LayoutInflater layoutInflater;  
    private Context context;  
    
 	private List<TAppFjxxb> valList;
 	
 	private List<Item> fjlxList;

    public CjkhFjWscListViewAdapter(Context context, List<TAppFjxxb> valList) {  
        this.context = context;  
        this.valList = valList;
        
        layout_id = R.layout.item_cjkh_fjxxwsc;
    }  

    @Override 
    public int getCount() {  
        return valList.size();  
    }  

    @Override 
    public Object getItem(int position) {  
        return null;  
    }  

    @Override 
    public long getItemId(int position) {  
        return position;  
    }  

    @Override 
    public View getView(final int position, View convertView, ViewGroup parent) {  
        Holder holder = null;
        TAppFjxxb item = valList.get(position);
        if (convertView == null) {  
            layoutInflater = LayoutInflater.from(context);  
            convertView = layoutInflater.inflate(layout_id, null);  
            holder = new Holder();  
            holder.txt_fjmc = (TextView) convertView.findViewById(R.id.fjxx_item_fjmc);
            holder.txt_fjlx = (TextView) convertView.findViewById(R.id.fjxx_item_fjlx);
            holder.img = (ImageView) convertView.findViewById(R.id.fjxx_item_img);
            holder.txt_tbbs = (TextView) convertView.findViewById(R.id.fjxx_item_tbbs);
            holder.sc_button = (Button)convertView.findViewById(R.id.fjxx_sc_button);
            if (this.mOnCjjlDetailClickListener != null) {
		    	holder.sc_button.setOnClickListener(new OnClickListener(){

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						mOnCjjlDetailClickListener.clickListener(position);
					}
		    		
		    	});
		    }
            convertView.setTag(holder);  
        } else {  
            holder = (Holder) convertView.getTag();  
        }  
        if (holder != null) {  
            convertView.setId(position);  
            holder.setId(position);  
            holder.txt_fjmc.setText(item.getWjmc());  
            holder.txt_fjmc.setTag(item.getUuid());
            if(item.getSczt()==3){
            	holder.txt_tbbs.setText(context.getResources().getString(R.string.param260));
            	holder.txt_tbbs.setTextColor(Color.RED);
            }else if(item.getSczt()==1){
            	holder.txt_tbbs.setText(context.getResources().getString(R.string.param261));
            }
            if(null == item.getFjlx()){
                item.setFjlx(5);
            }
            if (fjlxList != null) {
				for (Item mItem : fjlxList) {
					if (mItem.getKey().equals(item.getFjlx().toString())) {
						if(!mItem.getValue().toString().equals("")){
						holder.txt_fjlx.setText(mItem.getValue());
						}else{
							holder.txt_fjlx.setText("");
						}
						break;
					}
				}
			}
            String fullPath = ToolUtil.getFileFullPath(ToolUtil.getFilePath(item.getUuid() + "." +item.getWjlx()));
            
            File file = new File(fullPath);
            holder.img.setImageResource(R.drawable.bank_tx);
            if(file.exists()) {			// 
            	Glide.with(context).load(fullPath).into(holder.img);
				try {
					Bimp.setCompressor(fullPath,holder.img);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
            }
	    }
        return convertView;  
    }  
    
    public class Holder {  
    	public TextView txt_fjmc;   
    	public TextView txt_fjlx;
    	public ImageView img;
    	public TextView txt_tbbs;
    	public Button sc_button;
    	void setId(int position) {  
    		txt_fjmc.setId(position);    
    	}  
    }
    
    public void setFjlx(List<Item> fjlxList) {
		this.fjlxList = fjlxList;
	}
    
    Handler handler = new Handler(new Handler.Callback() {
		
		@Override
		public boolean handleMessage(Message msg) {
			// TODO Auto-generated method stub
			switch (msg.what) {
				case 101:
					notifyDataSetChanged();
					break;
			}
			
			return false;
		}
	});
    private OnCjjlDetailClickListener mOnCjjlDetailClickListener;
	
	public void setOnCjjlDetailClickListener(OnCjjlDetailClickListener mOnCjjlDetailClickListener) {
		this.mOnCjjlDetailClickListener = mOnCjjlDetailClickListener;
	}
	
	public interface OnCjjlDetailClickListener {
		void clickListener(int position);
	}
}
